#include "picanimationwid.h"
#include"protreeitem.h"
#include<QPainter>

PicAnimationWid::PicAnimationWid(QWidget *parent) : QWidget(parent),
    _factor(0.0), _cur_item(nullptr), _b_start(false)
{
    _timer = new QTimer(this);
    connect(_timer, &QTimer::timeout, this, &PicAnimationWid::TimeOut);
}

PicAnimationWid::~PicAnimationWid()
{

}

void PicAnimationWid::SetPixmap(QTreeWidgetItem *item)
{
    if(!item)
    {
        return ;
    }
    auto *tree_item = dynamic_cast<ProTreeItem*>(item);
    auto path = tree_item->GetPath();
    _pixmap1.load(path);
    _cur_item = tree_item;

    if(_map_items.find(path) == _map_items.end())
    {
        _map_items[path] = tree_item;
        emit SigUpPreList(tree_item);
    }

    emit SigSelectItem(_cur_item);

    auto *next_item = tree_item->GetNextPicItem();
    if(!next_item)
    {
        return ;
    }
    auto next_path = next_item->GetPath();
    _pixmap2.load(next_path);
    if(_map_items.find(next_path) == _map_items.end())
    {
        _map_items[next_path] = next_item;
        emit SigUpPreList(next_item);
    }
}

void PicAnimationWid::Start()
{
    emit SigStart();
    emit SigStartMusic();
    _factor = 0;
//    每25毫秒渲染一次页面，即渐隐渐现，直到图片完全显示出来，再切换为下一张图片
    _timer->start(_timer_time);
    _b_start = true;
}

void PicAnimationWid::SlideNext()
{
    Stop();
    if(!_cur_item)
    {
        return ;
    }
    auto *cur_pro_item = dynamic_cast<ProTreeItem*>(_cur_item);
    auto *next_item = cur_pro_item->GetNextPicItem();
    if(!next_item)
    {
        return ;
    }
    SetPixmap(next_item);
    update();
}

void PicAnimationWid::SlidePre()
{
    Stop();
    if(!_cur_item)
    {
        return ;
    }
    auto *cur_pro_item = dynamic_cast<ProTreeItem*>(_cur_item);
    auto *pre_item = cur_pro_item->GetPrePicItem();
    if(!pre_item)
    {
        return ;
    }
    SetPixmap(pre_item);
    update();
}

void PicAnimationWid::SlotUpSelectShow(QString path)
{
    auto iter = _map_items.find(path);
    if(iter == _map_items.end())
    {
        return ;
    }
    UpSelectPixmap(iter.value());
    update();
}

void PicAnimationWid::SlotStartOrStop()
{
    if(!_b_start)
    {
        _factor = 0;
        _timer->start(_timer_time);
        _b_start = true;
        emit SigStartMusic();
    }
    else
    {
        _timer->stop();
        _factor = 0;
        update();
        _b_start = false;
        emit SigStopMusic();
    }
}

void PicAnimationWid::paintEvent(QPaintEvent *event)
{
    if(_pixmap1.isNull())
    {
        return;
    }
//    第一步：创建一个画笔，来渲染当前画面
    QPainter painter(this);
//    设置防锯齿效果，简单来说就是画面渲染更流畅
    painter.setRenderHint(QPainter::Antialiasing, true);
//    第二步：获取当前区域的大小，将图片缩放到这么大
    QRect rect = geometry();
    int w = rect.width();
    int h = rect.height();
//    设置等比例缩放，防止失真
    _pixmap1 = _pixmap1.scaled(w, h, Qt::KeepAspectRatio);
//    第三步：创建一个完全不透明的黑色图片，然后一点一点变成完全透明
    int alpha = 255 * (1.0f - _factor);
    QPixmap alphaPixmap(_pixmap1.size());
    alphaPixmap.fill(Qt::transparent);
//    第四步：再创建一个画笔，通过不同模式渲染这两个图片
    QPainter p1(&alphaPixmap);
//    设置画图的模式，可以查询帮助来了解具体功能
    p1.setCompositionMode(QPainter::CompositionMode_Source);
    p1.drawPixmap(0, 0, _pixmap1);
    p1.setCompositionMode(QPainter::CompositionMode_DestinationIn);
    p1.fillRect(alphaPixmap.rect(), QColor(0, 0, 0, alpha));
    p1.end();
//    第五步：将最终画出来的图片渲染在本UI界面上
    int x = (w - _pixmap1.width()) / 2;
    int y = (h - _pixmap1.height()) / 2;
    painter.drawPixmap(x, y, alphaPixmap);

    if(_pixmap2.isNull())
    {
        return ;
    }

    _pixmap2 = _pixmap2.scaled(w, h, Qt::KeepAspectRatio);
    alpha = 255 * (_factor);
    QPixmap alphaPixmap2(_pixmap2.size());
    alphaPixmap2.fill(Qt::transparent);
    QPainter p2(&alphaPixmap2);
    p2.setCompositionMode(QPainter::CompositionMode_Source);
    p2.drawPixmap(0, 0, _pixmap2);
    p2.setCompositionMode(QPainter::CompositionMode_DestinationIn);
    p2.fillRect(alphaPixmap2.rect(), QColor(0, 0, 0, alpha));
    p2.end();
    x = (w - _pixmap2.width()) / 2;
    y = (h - _pixmap2.height()) / 2;
    painter.drawPixmap(x, y, alphaPixmap2);

}

void PicAnimationWid::Stop()
{
    emit SigStop();
    emit SigStopMusic();
    _timer->stop();
    _factor = 0;
    _b_start = false;
}

void PicAnimationWid::UpSelectPixmap(QTreeWidgetItem *item)
{
    if(!item)
    {
        return;
    }
    auto* tree_item = dynamic_cast<ProTreeItem*>(item);
    auto path = tree_item->GetPath();
    _pixmap1.load(path);
    _cur_item = tree_item;
    if(_map_items.find(path) == _map_items.end())
    {
        _map_items[path] = tree_item;
    }

    auto* next_item = tree_item->GetNextPicItem();
    if(!next_item)
    {
        return;
    }
    auto next_path = next_item->GetPath();
    _pixmap2.load(next_path);
    if(_map_items.find(next_path) == _map_items.end())
    {
        _map_items[next_path] = next_item;
    }
}

void PicAnimationWid::TimeOut()
{
    if(!_cur_item)
    {
        Stop();
        update();
        return ;
    }

    _factor += 0.01;
//    当这张图片完全显示出来的时候，就可以切换下一张图片了
    if(_factor >= 1)
    {
        _factor = 0;
        auto *cur_pro_item = dynamic_cast<ProTreeItem*>(_cur_item);
        auto *next_pro_item = cur_pro_item->GetNextPicItem();
        if(!next_pro_item)
        {
            Stop();
            update();
            return ;
        }
        SetPixmap(next_pro_item);
        update();
        return ;
    }
    update();
}
